<?php
namespace app\admin\controller;
use think\Db;
use think\Controller;
use think\captcha\Captcha;

class Login extends Controller
{
    public function login()
    {	
    	$info = session("info");
    	if($info){
    		$this->redirect("Index/index");
    	}
        return $this->fetch();
    }

    public function do_login()
    {
    	$account = input("post.account");
		$passwd  = input("post.passwd");
		$code  = input("post.code");
		if($account==""){
			$this->error("账号不能为空");
		}
		if($passwd==""){
			$this->error("密码不能为空");
		}
		if(!$this->check_verify($code))$this->error("验证码有误!");
		$data = db('admin')->where(['account'=>$account])->find();
		
		if(!$data){
			$this->error("账号或者密码错误！");
		}elseif($data['staus']==0){
			$this->error("该账号被禁止使用!");
		}else{
			if($data['password']!=md5($passwd)){
				$this->error("账号或者密码错误！");
			}else{
				$condition['a.id']=$data['id'];
				$data['head_image'] = IMG_PATH.$data['head_image'];
				session("info",$data);
				$l['ip']   = get_client_ip();
				$l['logintime'] = time();
				db('admin')->where(['account'=>$account])->update($l);
				$this->success('登录成功','Index/Index');
			}
		}
    }

    public function up_login(){
    	session("info",null);
    }

    public function captchaImg()
	{	$config =    [
			'codeSet'	  =>'1234567890',
		// 验证码字体大小
			'fontSize'    =>    30,
		// 验证码位数
			'length'      =>    4,
		// 关闭验证码杂点
			'useNoise'    =>    true,
	];
		$captcha = new Captcha($config);
		return $captcha->entry();
	}

	/**
	 * 检测输入的验证码是否正确
	 *
	 * @param $code 为用户输入的验证码字符串，
	 * @param $id 多个验证码标识
	 *
	 */
	function check_verify($code, $id = ''){
		$captcha = new Captcha();
		return $captcha->check($code, $id);
	}
}
